﻿using SlamDunk.CMS.DAL;
using SlamDunk.CMS.Model.DataModel;
using System;
using System.Collections.Generic;

namespace SlamDunk.CMS.BLL
{
    public class ChannelBll
    {
        #region 01.根据条件获取记录条数
        /// <summary>
        /// 根据条件获取记录条数
        /// </summary>
        /// <param name="arg1">条件参数</param>
        /// <param name="arg2">条件参数</param>
        /// <param name="arg3">条件参数</param>
        /// <param name="arg4">条件参数</param>
        /// <returns>记录条数</returns>
        public int GetCount(string arg1, string arg2, string arg3, string arg4)
        {
            //1个参数
            if (!string.IsNullOrWhiteSpace(arg1) && string.IsNullOrWhiteSpace(arg2) && string.IsNullOrWhiteSpace(arg3) && string.IsNullOrWhiteSpace(arg4))
            {
                return this.CurrentDal.GetCount("Field1", arg1);
            }

            //2个参数
            if (!string.IsNullOrWhiteSpace(arg1) && !string.IsNullOrWhiteSpace(arg2) && string.IsNullOrWhiteSpace(arg3) && string.IsNullOrWhiteSpace(arg4))
            {
                return this.CurrentDal.GetCount("Field1", arg1, "Field2", arg2);
            }

            //3个参数
            if (!string.IsNullOrWhiteSpace(arg1) && !string.IsNullOrWhiteSpace(arg2) && !string.IsNullOrWhiteSpace(arg3) && string.IsNullOrWhiteSpace(arg4))
            {
                return this.CurrentDal.GetCount("Field1", arg1, "Field2", arg2, "Field3", arg3);
            }

            //4个参数
            return this.CurrentDal.GetCount("Field1", arg1, "Field2", arg2, "Field3", arg3, "Field4", arg4);
        }

        #endregion

        #region 03.根据父Id查询所有栏目列表
        /// <summary>
        /// 根据父Id获取栏目列表
        /// </summary>
        /// <param name="pId">父Id</param>
        /// <returns>栏目列表</returns>
        public List<Channel> GetModelList(int pId)
        {
            return this.CurrentDal.GetModelList(pId);
        }
        #endregion

        #region 04.根据栏目对象获取文章总页数
        /// <summary>
        /// 根据栏目对象获取文章总页数
        /// </summary>
        /// <param name="model">栏目对象</param>
        /// <returns>符合条件的文章页数</returns>
        public int GetPageCount(Channel model)
        {
            ArticleBll ArticleBll = new ArticleBll();
            int rowCount = ArticleBll.GetCount(model.Id);
            int pageCount = (int)Math.Ceiling(rowCount * 1.0 / model.PageSize);
            return pageCount;
        }
        #endregion

        #region 01.实例化数据层
        /// <summary>
        /// 实例化数据层
        /// </summary>
        private readonly ChannelDal CurrentDal = new ChannelDal();
        #endregion

        #region 02.添加
        /// <summary>
        /// 添加一个实体对象
        /// </summary>
        /// <param name="channel">构造好要添加的实体对象</param>
        /// <returns>添加完毕后的实体对象</returns>
        public Channel Add(Channel channel)
        {
            return CurrentDal.Add(channel);
        }
        #endregion

        #region 03.删除（物理删除）
        /// <summary>
        /// 删除一个实体对象
        /// </summary>
        /// <param name="id">要删除的实体对象Id</param>
        /// <returns>删除结果</returns>
        public bool PhysicalDelete(int id)
        {
            return CurrentDal.PhysicalDelete(id) > 0;
        }
        #endregion

        #region 05.修改
        /// <summary>
        /// 修改一个实体对象
        /// </summary>
        /// <param name="channel">构造好的要修改的实体对象</param>
        /// <returns>修改结果</returns>
        public bool Update(Channel channel)
        {
            return CurrentDal.Update(channel) > 0;
        }
        #endregion

        #region 06.根据主键获取单个实体对象
        /// <summary>
        /// 根据主键获取单个实体对象
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>单个实体对象</returns>
        public Channel GetModel(int id)
        {
            return CurrentDal.GetModel(id);
        }
        #endregion

        #region 07.获取实体对象集合
        /// <summary>
        /// 获取所有实体对象
        /// </summary>
        /// <returns>实体对象集合</returns>
        public List<Channel> GetModelList()
        {
            return CurrentDal.GetModelList();
        }
        #endregion

        #region 08.获取总记录条数
        /// <summary>
        /// 返回总记录条数
        /// </summary>
        /// <returns>总记录条数</returns>
        public int GetCount()
        {
            return CurrentDal.GetCount();
        }
        #endregion
    }
}
